package com.iseastar.guojiang;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.umeng.analytics.MobclickAgent;

import droid.frame.App;
import droid.frame.Config;
import droid.frame.activity.ActivityMgr;
import droid.frame.fragment.FragmentMgr;
import droid.frame.fragment.FrameBaseFragment;
import droid.frame.push.PushInterface;
import droid.frame.ui.dialog.AppLoading;
import droid.frame.utils.android.DataCleanManager;
import droid.frame.utils.android.SharedPref;

/**
 * 非public.用的时候直接拷贝到子项目中。用法同 {@link BaseActivity} 一样
 *
 * @author coffee<br>
 * 2014年9月19日下午3:42:50
 */
public abstract class BaseFragment extends FrameBaseFragment {

    private ProgressBar mProgressBar;
    private TextView mEmptyView;

    protected SwipeRefreshLayout mSwipeLayout;

    @Override
    protected void findViewById() {
        super.findViewById();
        getAppTitle().setBackDrawable(R.drawable.title_back);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    }

    protected void onRefreshComplete() {
        if (mSwipeLayout != null) {
            getActivityContext().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mSwipeLayout.setRefreshing(false);
                }
            });
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getApp() != null) {
            Thread.setDefaultUncaughtExceptionHandler(getApp().getUncaughtExceptionHandler());
        }
        MobclickAgent.onResume(getContext());
    }

    @Override
    public void onPause() {
        super.onPause();
        // MobclickAgent.onPause(getContext());
    }

    public boolean isEmpty(Object str) {
        if (str == null || str.toString().trim().length() == 0 || "null".equals(str)) {
            return true;
        } else {
            return false;
        }
    }

    public BeeApplication getApp() {
        return (BeeApplication) BeeApplication.getContext();
    }

    /**
     * 检测是否在其他设备上进行过登录
     *
     * @param result
     * @return
     */
    public boolean checkLoginStatus(ReqResult result) {
        if (result.isSuccess()) {
            return true;
        }
        //其他设备已经登录了
        else if (result.getStatus() == 10000) {
            try {
                AppLogic.logoutDealService(getContext());
                PushInterface.logout();
            } catch (Exception e) {
                e.printStackTrace();
            }
            new DataCleanManager().cleanApplicationData(getActivityContext(), Config.getMainDiar());
            AppCache.setUser(null);
            AppCache.setOrder(null);
            AppCache.setStationOpenInfoBean(null);
            ActivityMgr.finishAll();
            Intents.startLauncherActivity(getActivityContext());
            return false;
        } else {
            return false;
        }
    }

    public void showLoadingBar() {
        if (mProgressBar != null) {
            mProgressBar.setVisibility(View.VISIBLE);
        }
    }

    public void cancelLoadingBar() {
        getActivityContext().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressBar != null) {
                    mProgressBar.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 该方法主要考虑到一个界面里多个EmptyView
     *
     * @param emptyText
     */
    protected void setEmptyTextView(TextView emptyText) {
        this.mEmptyView = emptyText;
    }

    public void setEmptyView(ViewGroup viewGroup, String emptyText) {
        if (mEmptyView == null) {
            return;
        }
        //
        if (viewGroup instanceof ListView || viewGroup instanceof GridView) {
            AbsListView mAbsListView = (AbsListView) viewGroup;
            if (mAbsListView == null || mAbsListView.getAdapter() == null || mAbsListView.getAdapter().getCount() == 0) {
                ((View) mEmptyView.getParent()).setVisibility(View.VISIBLE);
                if (isEmpty(emptyText) == false) {
                    mEmptyView.setText(emptyText);
                }
            } else {
                ((View) mEmptyView.getParent()).setVisibility(View.GONE);
            }
        }
    }

    private AppLoading loading;

    public void showLoadingDialog(final String dialogMessage) {
        if (getActivityContext() != null) {
            getActivityContext().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (loading != null && loading.isShowing()) {
                        loading.setMessage(dialogMessage);
                        return;
                    } else {
                        loading = new AppLoading(getActivityContext());
                        loading.setMessage(dialogMessage);
                        loading.show();
                    }
                }
            });
        }
    }

    protected void cancelLoadingDialog() {
        getActivityContext().runOnUiThread(new Runnable() {
            public void run() {
                if (loading != null) {
                    loading.dismiss();
                }
            }
        });
    }

    /**
     * {@link BaseActivity#showToast(String)}
     *
     * @param message
     */
    public void showToast(final String message) {
        App.getHandler().post(new Runnable() {
            @Override
            public void run() {
                final Toast toast = Toast.makeText(getActivityContext(), message, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });

    }

    public void setSharedPref(String key, String value) {
        SharedPref.put(key, value);
    }

    protected String getSharedPref(String key) {
        return SharedPref.get(key);
    }

    public void startActivity(Class<?> classActivity) {
        Intent intent = new Intent();
        intent.setClass(getActivityContext(), classActivity);
        startActivity(intent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        FragmentMgr.getInstance((FragmentActivity) getActivityContext()).onDestroy((FragmentActivity) getActivityContext());
    }
}
